
const express = require("express");
const router = express.Router();
const passport = require("passport"); //express的兼容身份验证插件

const Profile = require("./../../models/Profile");

//接口测试
router.get("/test", (req, res) => {
  res.json({msg: "测试成功了哦"});
});

/**
 * @route  GET请求 api/profiles
 * @desc  获取所有信息
 * @access  Private
 */
router.get("/", passport.authenticate("jwt", {session: false}), (req, res) => {
  Profile.find()
    .then(profile => {
      if(!profile) {
        return res.status(404).json("没有任何内容");
      }
      res.json(profile);
    })
    .catch(err => res.status(404).json(err));
});

/**
 * @route  GET请求 api/profiles/:id
 * @desc  获取单个信息
 * @access  Private
 */
router.get("/:id", passport.authenticate("jwt", {session: false}), (req, res) => {
  Profile.findOne({_id: req.params.id})
    .then(profile => {
      if(!profile) {
        return res.status(404).json("没有任何内容");
      }
      res.json(profile);
    })
    .catch(err => res.status(404).json(err));
});

/**
 * @route  POST请求 api/profiles/add
 * @desc  创建信息接口
 * @access  Private
 */
router.post("/add", passport.authenticate("jwt", {session: false}), (req, res) => {
  const profileFields = {};
  if (req.body.type) profileFields.type = req.body.type;
  if (req.body.describe) profileFields.describe = req.body.describe;
  if (req.body.income) profileFields.income = req.body.income;
  if (req.body.expend) profileFields.expend = req.body.expend;
  if (req.body.cash) profileFields.cash = req.body.cash;
  if (req.body.remark) profileFields.remark = req.body.remark;
  new Profile(profileFields).save().then(profile => {
    res.json(profile);
    // console.log(profile);
  });
});

/**
 * @route  POST请求 api/profiles/edit
 * @desc  编辑信息接口
 * @access  Private
 */
router.post("/edit/:id", passport.authenticate("jwt", {session: false}), (req, res) => {
  const profileFields = {};
  if (req.body.type) profileFields.type = req.body.type;
  if (req.body.describe) profileFields.describe = req.body.describe;
  if (req.body.income) profileFields.income = req.body.income;
  if (req.body.expend) profileFields.expend = req.body.expend;
  if (req.body.cash) profileFields.cash = req.body.cash;
  if (req.body.remark) profileFields.remark = req.body.remark;

  Profile.findOneAndUpdate(
    {_id: req.params.id},
    {$set: profileFields},
    {new: true}
  ).then(profile => {
    res.json(profile);
    // console.log(profile);
  });
});

/**
 * @route  POST请求 api/profiles/delete/:id
 * @desc  删除信息接口
 * @access  Private
 */
router.delete("/delete/:id", passport.authenticate("jwt", {session: false}), (req, res) => {
  Profile.findOneAndRemove({_id: req.params.id}, {new: true})
    .then(profile => {
      res.json({msg: "删除成功"});
      // console.log(profile);
      // ⭐坑：findOneAndRemove这里的回调返回的profile并不是整个表，而是表中的这个目标文档/行
      // 所以下面的save()并不是保存新的表，而还是被删除的目标文档/行，所以会报错
      // profile.save().then(profile => res.json(profile));
    })
    .catch(err => res.status(404).json("删除失败"));
});

module.exports = router;